<?php
/**
 * +--------------------------------------------------------+
 * @Created by PhpStorm.
 * @Category FileController.php
 * @Depiction:
 * Author: hwy
 * Date: 2020/3/2  17:33
 * +--------------------------------------------------------+
 */

namespace App\Http\Controllers\Admin;

use App\Models\File;
use Illuminate\Http\Request;
use App\Handlers\FileUploadHandler;
use App\Http\Controllers\Controller;

class FileController extends Controller
{
    public function index(Request $request){

        $responseData = File::getList($request);

        return response()->json($responseData);
    }


    // 上传文件
    public function upload(Request $request, FileUploadHandler $uploader)
    {
        $key = rand(1000,9999);
        $api_key = 'b9b02d17a72468d0571e434d9f4db806';
        // Log::info('上传到值',$_SERVER);
        if (empty($key)) {

            return response()->json(['code' => -1, 'message'=>'error']);
        }

        if ($api_key != "b9b02d17a72468d0571e434d9f4db806") {
            return response()->json(['code' => -1, 'message'=>'error']);
        }

        if ($request->file) {
            $file_name = $key;
            $result = $uploader->save($request->file, 'files', $file_name);
            if ($result)
                return response()->json(['code' => 0, 'data'=>[
                    'file_name' => $result->file_name,
                    'file_size' => $result->file_size,
                    'file_path' => $result->add_path,
                    'file_id' => $result->id,
                ]]);
        }

        return response()->json(['code' => -1, 'message'=>'error']);
    }
}
